package org.example.video.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commons.common.Result;
import com.example.commons.entity.Barrage;
import com.example.commons.entity.Video;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.video.service.BarrageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "视频弹幕 前端控制器")
@RestController
@RequestMapping("/barrage")
public class BarrageController {
    private final BarrageService barrageService;


    /**
     * 新增
     *
     * @param barrage
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Barrage barrage) {
        barrage.setUid(StpUtil.getLoginIdAsLong());
        barrageService.save(barrage);
        return Result.success();
    }


    /**
     * 修改
     *
     * @param barrage
     * @return
     */
    @Operation(summary = "修改")
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id, @RequestBody Barrage barrage) {
        return Result.success(barrageService.updateById(barrage));
    }


    /**
     * 查询所有Barrage
     *
     * @return
     */
    @Operation(summary = "查询所有Barrage")
    @GetMapping
    public Result findAll() {
        return Result.success(barrageService.list());
    }


    /**
     * 获取单个
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个")
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(barrageService.getById(id));
    }

    /**
     * 分页显示
     *
     * @param
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页显示")
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String content,
                           @RequestParam(defaultValue = "") Long uid,
                           @RequestParam(defaultValue = "") Long vid,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Barrage> queryWrapper = new LambdaQueryWrapper();

        if (StrUtil.isNotEmpty(content)) {
            // name 不为空且不是空字符串的处理逻辑
            queryWrapper.like(Barrage::getContent, content);
        }
        if (uid != null && uid != 0) {
            // name 不为空且不是空字符串的处理逻辑
            queryWrapper.eq(Barrage::getUid, uid);
        }
        if (vid != null && vid != 0) {
            // name 不为空且不是空字符串的处理逻辑
            queryWrapper.eq(Barrage::getVid, vid);
        }

        queryWrapper.orderByDesc(Barrage::getId);
        Page<Barrage> page = barrageService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Barrage> records = page.getRecords();
        page.setRecords(records);
        return Result.success(page);
    }


    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "单个删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(barrageService.removeById(id));
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/batch/{ids}")
    @Transactional
    public Result deleteByIds(@PathVariable String[] ids) {
        return Result.success(barrageService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 根据vid查询视频
     *
     * @param vid
     * @return
     */
    @GetMapping("/video/{vid}")
    public Result getByVideoId(@PathVariable Long vid) {
        return Result.success(barrageService.getByVideoId(vid));
    }

    public BarrageController(BarrageService barrageService) {
        this.barrageService = barrageService;
    }
}
